//
//  AllOrdersInTheRestaurantViewController.m
//  Park
//
//  Created by 123456 on 2017/12/11.
//  Copyright © 2017年 YTYangK. All rights reserved.
//

#import "AllOrdersInTheRestaurantViewController.h"
#import "RestaurantViewModel.h"
#import "AllOrdersInTheRestaurantModel.h"
#import "AllOrdersInTheRestaurantTableViewCell.h"
@interface AllOrdersInTheRestaurantViewController ()<AllOrdersInTheRestaurantTableViewCellDelegate>
{
    // 需要其他控件控制刷新
    MJRefreshNormalHeader* header;
    MJRefreshBackNormalFooter* footer;
    int _page;
    BOOL RefreshStatus;
    int b;//请求变量
    
}
@end

@implementation AllOrdersInTheRestaurantViewController


- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [header beginRefreshing];
        
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.isToSearchUpdate=YES;

    //注册cell
    [self.AllOrdersInTheRestaurantTableview registerNib:[UINib nibWithNibName:@"AllOrdersInTheRestaurantTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    self.AllOrdersInTheRestaurantTableview.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    
    self.AllOrdersInTheRestaurantTableview.estimatedRowHeight = 227;
    self.AllOrdersInTheRestaurantTableview.rowHeight = UITableViewAutomaticDimension;
    
    [self refreshTableView];
    

}

- (void)refreshTableView {
    
    header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _page = 0;
        [self RequestData:_page];
        self.isToSearchUpdate=YES;
    }];
    header.automaticallyChangeAlpha = YES;
    header.lastUpdatedTimeLabel.hidden = YES;
    [header beginRefreshing];
    self.AllOrdersInTheRestaurantTableview.header = header;
    
    footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        b=_page+19;
        b++;
        _page = b;
        [self RequestData:_page];
        self.isToSearchUpdate=NO;
    }];
    self.AllOrdersInTheRestaurantTableview.footer = footer;
    
}




-(void)RequestData:(int)page{
    
    [RestaurantViewModel requestWithUrlForAllOrdersInTheRestaurant:self andParam:@{@"token":[NSUD objectForKey:@"token"],@"iDisplayStart":@(page),@"iDisplayLength":@"20"} success:^(NSMutableArray *model) {
        
        [header endRefreshing];
        [footer endRefreshing];
        
        
        NSMutableArray *scheduingAry = [AllOrdersInTheRestaurantModel mj_objectArrayWithKeyValuesArray:model];

        if (self.isToSearchUpdate) {
            [self.dataArr removeAllObjects];
            for (NSDictionary *dic  in scheduingAry) {
                [self.dataArr addObject: dic];
            }
        }else{
            
            for (NSDictionary *dic in scheduingAry) {
                [self.dataArr addObject:dic];
            }
        }
    
        [self.AllOrdersInTheRestaurantTableview reloadData];

    } failure:^(NSString *error) {
        
        [header endRefreshing];
        [footer endRefreshing];
        [MBProgressHUD showError:error toView:self.view];
        
    }];
    
    
    
    
}

#pragma mark -  TableView DataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView {
    return 1;
}


- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}

- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath {
    return UITableViewAutomaticDimension;
}


- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
    
    AllOrdersInTheRestaurantTableViewCell *cell  = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.modelCellData =  self.dataArr[indexPath.row];
    cell.delegate =self;
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}


-(NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [[NSMutableArray alloc]init];
    }
    
    return  _dataArr;
}


-(void)confirm:(UIButton *)button{
    
    AllOrdersInTheRestaurantModel *list = self.dataArr[[self.AllOrdersInTheRestaurantTableview indexPathForCell:((AllOrdersInTheRestaurantTableViewCell *)[[button   superview]superview])].row];
    
    
    NSLog(@"MyRow:%@",list.id);
    
    [self reserveAffirm:list.id state:0];

    
}

-(void)cancel:(UIButton *)button{
    
    AllOrdersInTheRestaurantModel *list = self.dataArr[[self.AllOrdersInTheRestaurantTableview indexPathForCell:((AllOrdersInTheRestaurantTableViewCell *)[[button   superview]superview])].row];
    
    
    NSLog(@"MyRow:%@",list.id);
    
    [self reserveAffirm:list.id state:1];

}


-(void)reserveAffirm:(NSString *)id state:(int)i
{
    
    [RestaurantViewModel requestWithUrlForConfirmOrder:self andParam:@{@"orderId":@(id.integerValue),@"state":@(i)} success:^(NSMutableArray *model) {
        
        if(_page>=20){
            [header beginRefreshing];

        }else{
            
            [self RequestData:_page];

        }
        
    } failure:^(NSString *error) {
        
       [MBProgressHUD showError:error toView:self.view];

    }];
    
    
}



@end
